package com.study.hadoop.mapreduce.outputformat;

import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

import java.io.IOException;

/**
 * @Author huang
 * @Date 2022/1/3 20:16
 * @Version 1.0
 */
public class LogRecordWriter extends RecordWriter<Text, NullWritable> {

    private FSDataOutputStream atguigu;
    private FSDataOutputStream other;

    public LogRecordWriter(TaskAttemptContext job) {
        try {
            FileSystem fileSystem = FileSystem.get(job.getConfiguration());
            //创建两条流
            atguigu = fileSystem.create(new Path("D:\\生活\\study\\hadoop\\系统\\test\\atguigu.txt"));
            other = fileSystem.create(new Path("D:\\生活\\study\\hadoop\\系统\\test\\other.txt"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void write(Text key, NullWritable value) throws IOException, InterruptedException {
        //写入文件中
        String log = key.toString();
        if (log.equals("atguigu"))
            atguigu.writeBytes(log+"\n");
        else
            other.writeBytes(log+"\t");
    }

    @Override
    public void close(TaskAttemptContext context) throws IOException, InterruptedException {
        //关闭流
        IOUtils.closeStreams(atguigu,other);
    }
}
